<?php
/**
* 
*/
class CoachModel extends MY_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$this->table = 'nmp_coach';
	}

	//获取列表数据
	public function get_list_data($term='',$page=1,$size=50)
	{
		//查询搜索
		$where = ' where 1 ';
		if (!empty($term['venue'])) {
			$where .= 'AND c.shop_id ='.$term['venue'];
		}

		if (isset($term['keyword']) && !empty($term['keyword'])) {

			$where .= " AND (c.nickname LIKE '%".$term['keyword']."%' OR c.mobile LIKE '%".$term['keyword']."%')";
		}

		$query = $this->db->query("SELECT c.id, c.shop_id, c.nickname, c.mobile, c.sex, c.headimg, c.is_sort, s.name as venue FROM ".$this->table." as c LEFT JOIN nmp_shop as s ON c.shop_id=s.id ".$where." ORDER BY c.id DESC LIMIT ".($page-1)*$size.",".$size);

		$data = $query->result_array();

		foreach ($data as $key => $value) {
			$data[$key]['sex'] = $value['sex'] == 0 ? '未知' : ($value['sex'] == 1 ? '男' : '女');
		}

		$query = $this->db->query("select COUNT(*)  as num from ".$this->table." as c".$where);
		$row = $query->row_array();

		//总数
		$count = $row['num'];
		//总页数
		$pages = ceil($count/$size);
		
		return array('list'=>$data,'count'=>$count, 'pages'=>$pages);
	}


	//获取单行数据
	public function get_cocah_row($coach_id = '')
	{
		$this->db->select("* ");
		$this->db->from($this->table);
		$this->db->where('id', $coach_id);

		$query = $this->db->get();

		return $query->row_array();
	}


	//获取场馆
	public function get_venue_list()
	{
		$this->db->select("id,name");
		$this->db->from('nmp_shop');

		$query = $this->db->get();

		$data = $query->result_array();

		return $data;
	}


	//添加入库
	public function inser_coach_data($arr=array())
	{
		$arr['addtime'] = time();
		if ($this->db->insert($this->table,$arr)) 
		{
			return true;
		}
		else
		{
			return false;
		}

	}

	//更新数据
	public function updata_coach_data($arr =array(),$coach_id ='')
	{
		if ($this->db->update($this->table,$arr,array('id'=>$coach_id))){			
			return true;
		}else{
			return false;
		}

	}



	//获取统计信息
	public function coach_count($coach_id)
	{
		//本月新增预约
		$start = date('Y-m-01 00:00:00');
		$end = date('Y-m-d H:i:s');
		$query = $this->db->query("SELECT count(*) as m_num FROM nmp_user_appoint WHERE addtime >= unix_timestamp('$start') AND addtime <= unix_timestamp('$end') AND mid = '$coach_id'");
		$res = $query->row_array();
		$data['m_num'] = $res['m_num'];

		//本周新增预约
		$query = $this->db->query("SELECT count(user_id) as w_num FROM nmp_user_appoint WHERE YEARWEEK(FROM_UNIXTIME(addtime, '%Y-%m-%d %H:%i:%s') ,1) = YEARWEEK(now(),1) AND mid = '$coach_id'");
		$res = $query->row_array();
		$data['w_num'] = $res['w_num'];

		//上课数
		$query = $this->db->query("SELECT count(*) as attend_num FROM nmp_user_appoint WHERE addtime >= unix_timestamp('$start') AND addtime <= unix_timestamp('$end') AND mid = '$coach_id' AND status = 1");
		$res = $query->row_array();
		$data['attend_num'] = $res['attend_num'];

		//预约率
		$query = $this->db->query("SELECT count(*) as member_num FROM nmp_user");
		$user = $query->row_array();

		$query = $this->db->query("SELECT count(distinct(user_id)) as appoint_num FROM nmp_user_appoint WHERE addtime >= unix_timestamp('$start') AND addtime <= unix_timestamp('$end') AND mid = '$coach_id'");
		$appoint = $query->row_array();

		$data['app_percen'] = round($appoint['appoint_num']/$user['member_num']*100,2)."％";

		return $data;
	}


	//获取折线图数据
	public function appoint_data($coach_id,$dtnum = 30)
	{
		$date_arr = array();
		$appoint_arr = array();

		if (!is_numeric($dtnum)) 
		{
			//处理日期
			$d1 = substr($dtnum,0,10);
			$d2 = substr($dtnum,-10);
			$dlist = $this->getDateFromRange($d1,$d2);

			for ($i=0; $i < count($dlist); $i++) 
			{
				$date_arr[] = substr($dlist[$i],-5);

				$start = date('Y-m-d 00:00:00',strtotime($dlist[$i]));
				$end = date('Y-m-d 00:00:00',strtotime($dlist[$i])+86400);

				$query = $this->db->query("SELECT count(*) as appoint_num FROM nmp_user_appoint WHERE addtime >= unix_timestamp('$start') AND addtime <= unix_timestamp('$end') AND mid = '$coach_id'");
				$res = $query->row_array();

				$appoint_arr[] = $res['appoint_num'];
			}
		}
		else
		{
			for ($i=$dtnum; $i > 0; $i--) 
			{
				$date_arr[] = date('m-d',strtotime('-'.$i.' day'));

				$start = date('Y-m-d 00:00:00',strtotime('-'.$i.' day'));
				$end = date('Y-m-d 00:00:00',strtotime('-'.($i-1).' day'));

				$query = $this->db->query("SELECT count(*) as appoint_num FROM nmp_user_appoint WHERE addtime >= unix_timestamp('$start') AND addtime <= unix_timestamp('$end') AND mid = '$coach_id'");
				$res = $query->row_array();

				$appoint_arr[] = $res['appoint_num'];
			}

		}

		$data['date_list'] = $date_arr;
		$data['appoint_num'] = $appoint_arr;

		return $data;

	}


	/**
	 * 获取指定日期段内每一天的日期
	 * @param  Date  $startdate 开始日期
	 * @param  Date  $enddate   结束日期
	 * @return Array
	 */
	function getDateFromRange($startdate, $enddate)
	{
	    $stimestamp = strtotime($startdate);
	    $etimestamp = strtotime($enddate);

	    // 计算日期段内有多少天
	    $days = ($etimestamp-$stimestamp)/86400+1;

	    // 保存每天日期
	    $date = array();
	    for($i=0; $i<$days; $i++){
	        $date[] = date('Y-m-d', $stimestamp+(86400*$i));
	    }

	    return $date;
	}


	//预约记录
	public function bespeak_data($coach_id)
	{
		$query = $this->db->query("SELECT ap.*,c.name,ch.nickname,c.type_id FROM nmp_user_appoint as ap 
			LEFT JOIN nmp_course as c ON ap.cid = c.id 
			JOIN nmp_coach ch ON ap.mid = ch.id WHERE ap.mid = '$coach_id' ORDER BY ap.addtime DESC");
		$res = $query->result_array();

		foreach ($res as $key => $value) 
		{
			$res[$key]['addtime'] = date('Y-m-d',$value['addtime']);
			$res[$key]['status'] = $value['status'] == 1 ? '<font style="color:#0cc3aa">已签到</font>' : ($value['status'] == 2 ? '<font style="color:#ccc">已取消</font>' : '<font style="color:#fabf2d">未签到</font>');
			$res[$key]['type'] = $value['type_id'] == 1 ? '团教' : '私教';
		}


		return $res;

	}


	//
	public function user_data($coach_id)
	{
		$query = $this->db->query("SELECT u.id as user_id, u.nickname, u.mobile, c.nickname as coach_name FROM nmp_user_appoint as ap 
			LEFT JOIN nmp_user as u ON ap.user_id = u.id 
			JOIN nmp_coach as c ON ap.mid = c.id 
			WHERE ap.mid = '$coach_id' GROUP BY ap.user_id ORDER BY ap.addtime DESC");
		$res = $query->result_array();

		return $res;

	}


	//删除
	public function remove_coach($coach_id)
	{
		return $this->db->delete($this->table, array('id'=>$coach_id));
	}


}

?>